package com.zhl.test1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test1 {
    public static List<People> peoples = new ArrayList<>();
    public static void main(String[] args) {
        //目标：完成幸存者编号
        //由于每个囚犯是罪犯，有编号和位置，所以定义成对象进行实现
        //创建100个囚犯依次占位。
        Random r = new Random();


            for (int i = 1; i <= 100; i++) {
                while (true) {
                    int code = r.nextInt(200)+1;
                    //判断编号是否重复
                    if(iscanUse(code)){
                        People people = new People(code, i);
                        peoples.add(people);
                        break;//表示添加成功，退出循环
                    }
                }

            }

        System.out.println("囚犯占位："+peoples);
        //删除编号为奇数的囚犯
        while(peoples.size() > 1){
            List<People> temp = new ArrayList<>();
            for (int i = 1; i < peoples.size(); i+=2) {
                People people = peoples.get(i);
                temp.add(people);
            }
            peoples = temp;
        }
        People Luckpeople = peoples.get(0);
        System.out.println("幸存者编号："+Luckpeople);


    }

    public static boolean iscanUse(int code ){
        for (People people : peoples) {
            if(people.getCode() == code){
                return false;//编号重复，不能使用
            }
        }
        return true;

    }
}





